import Layout from "./pages/layout/test2";
import { useRoutes } from "react-router-dom";
import { withKeepAlive } from "./components/keepalive-cpn";
const context = require.context("./pages", true, /\.js$/);
const children = [];
export const componentNameList = [];
context.keys().forEach((path, index) => {
  const componentName = path.split("/")[1];
  if (componentName === "layout") return;
  componentNameList.push(componentName);
  // importFn(path)拿到各个模块的信息.default拿到组件的信息
  const Component = context(path).default;
  if (Component.prototype.isKeepAlive) {
    const KeepCom = withKeepAlive(Component, { cacheId: componentName });
    children.push({
      path: componentName,
      element: <KeepCom></KeepCom>,
    });
  } else {
    children.push({
      path: componentName,
      element: <Component></Component>,
    });
  }
});

const routers = [
  {
    path: "/",
    element: <Layout />,
    children: children,
  },
];
const RouterLayout = () => {
  const route = useRoutes(routers);
  return <>{route}</>;
};
export default RouterLayout;
